package com.zhenzhen.xszhcp.controller;

import com.zhenzhen.xszhcp.model.*;
import com.zhenzhen.xszhcp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class insertController {
    @Autowired
    private LogService logService;
    @Autowired
    private GroupinfoService groupinfoService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private KechengService kechengService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private SemesterService semesterService;
    @Autowired
    private ZhuangtaiService zhuangtaiService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private ZhcjService zhcjService;
    @Autowired
    private KcblService kcblService;

    /*添加用户组信息*/
    @RequestMapping("Insertyhzxxgl")
    public boolean Insertyhzxxgl(@RequestParam(value = "title") String title,
                                 HttpServletRequest httpServletRequest) {
        Groupinfo groupinfo = new Groupinfo();
        groupinfo.setGroupname(title);
        if (groupinfoService.insertGroupinfo(groupinfo) != 0) {
            setLog.setlod(httpServletRequest, "添加名字为" + title + "的用户组", logService);
            return true;
        } else
            return false;
    }

    /*添加学生并创建该学生的登录账户*/
    @RequestMapping("Insertxsxxgl")
    public boolean Insertxsxxgl(@RequestParam String sname,
                                @RequestParam String birth,
                                @RequestParam String cid,
                                @RequestParam String tel,
                                @RequestParam String idcard,
                                @RequestParam String xb,
                                HttpServletRequest httpServletRequest) throws ParseException {
        //判断数据库中是否存在该学号学生
        if (studentService.getStudentByIdcard(Integer.parseInt(idcard)) != null)
            return false;
        Student student = new Student();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date birthtime = sdf.parse(birth);
        student.setSname(sname);
        student.setScsny(birthtime);
        student.setSscid(Integer.parseInt(cid));
        student.setSphone(tel);
        student.setXuehao(Integer.parseInt(idcard));
        student.setSsex(xb);
        //添加学生
        if (studentService.insertStudent(student) != 0) {
            Users users = new Users();
            users.setUsername(idcard);
            users.setGroupid(3);
            users.setRegtime(new Date(new Date().getTime()));
            users.setUpassword(UtilPacket.Md5MD5String("123456"));
            //创建学生账号
            if (usersService.insertSelective(users) != 0)
                setLog.setlod(httpServletRequest, "成功为学号" + idcard + "创建账户", logService);
            setLog.setlod(httpServletRequest, "添加学号为" + idcard + "的学生", logService);
            return true;
        } else
            return false;
    }

    /*添加教师信息*/
    @RequestMapping("Insertjsxxgl")
    public boolean Insertjsxxgl(@RequestParam String tname,
                                @RequestParam String birth,
                                @RequestParam String zyzc,
                                @RequestParam String tel,
                                @RequestParam String idcard,
                                @RequestParam String xb,
                                HttpServletRequest httpServletRequest) throws ParseException {
        if (teacherService.getTeacherBytcard(Integer.parseInt(idcard)) != null)
            return false;
        Teacher teacher = new Teacher();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date birthtime = sdf.parse(birth);
        teacher.setTname(tname);
        teacher.setTbirth(birthtime);
        teacher.setTzc(zyzc);
        teacher.setTphone(tel);
        teacher.setTcard(Integer.parseInt(idcard));
        teacher.setTsex(xb);
        if (teacherService.insertTeacher(teacher) != 0) {
            Users users = new Users();
            users.setUsername(idcard);
            users.setGroupid(2);
            users.setRegtime(new Date(new Date().getTime()));
            users.setUpassword(UtilPacket.Md5MD5String("123456"));
            if (usersService.insertSelective(users) != 0)
                setLog.setlod(httpServletRequest, "成功为工号" + idcard + "创建账户", logService);
            setLog.setlod(httpServletRequest, "添加工号为" + idcard + "的教师", logService);
            return true;
        } else
            return false;
    }

    /*添加课程*/
    @RequestMapping("Insertkcxxgl")
    public boolean Insertkcxxgl(@RequestParam String kname,
                                @RequestParam int zxs,
                                @RequestParam int xf,
                                @RequestParam int teacher,
                                @RequestParam String kclx,
                                @RequestParam String kcsx,
                                @RequestParam String kcxz,
                                HttpServletRequest httpServletRequest){
        Kecheng kecheng = new Kecheng();
        kecheng.setXf(xf);
        kecheng.setZxs(zxs);
        kecheng.setKname(kname);
        kecheng.setKclx(kclx);
        kecheng.setKcsx(kcsx);
        kecheng.setKcxz(kcxz);
        kecheng.setTeacher(String.valueOf(teacher));
        if (kechengService.insertKecheng(kecheng) != 0) {
            setLog.setlod(httpServletRequest, "添加课程名为" + kname + "的课程", logService);
            return true;
        } else
            return false;
    }

    /*添加班级信息*/
    @RequestMapping("Insertbjxxgl")
    public boolean Insertbjxxgl(@RequestParam String bj,
                                @RequestParam String sstid,
                                @RequestParam String nj,
                                @RequestParam String zy,
                                @RequestParam String xy,
                                HttpServletRequest httpServletRequest){
        Classes classes = new Classes();
        classes.setNj(nj);
        classes.setZy(zy);
        classes.setXy(xy);
        classes.setBj(bj);
        classes.setSstid(Integer.parseInt(sstid));
        if (classesService.insertClasses(classes) != 0) {
            setLog.setlod(httpServletRequest, "添加名称为" + bj + "的班级", logService);
            return true;
        } else
            return false;
    }

    /*添加学期*/
    @RequestMapping("Insertxqxxgl")
    public boolean Insertxqxxgl(@RequestParam String xq,
                                @RequestParam String year,
                                HttpServletRequest httpServletRequest){
        Semester semester = new Semester();
        semester.setXq(xq);
        semester.setYear(year);
        if (semesterService.insertSemester(semester) != 0) {
            setLog.setlod(httpServletRequest, "添加名称为" + xq + "的学期信息", logService);
            return true;
        } else
            return false;
    }

    /*添加状态信息*/
    @RequestMapping("Insertztxxgl")
    public boolean Insertztxxgl(@RequestParam String ztname,
                                HttpServletRequest httpServletRequest){
        Zhuangtai zhuangtai = new Zhuangtai();
        zhuangtai.setZtname(ztname);
        if (zhuangtaiService.insertZhuangtai(zhuangtai) != 0) {
            setLog.setlod(httpServletRequest, "添加名称为" + ztname + "的状态信息", logService);
            return true;
        } else
            return false;
    }

    /*添加用户账号*/
    @RequestMapping("Insertyhzhgl")
    public boolean Insertyhzhgl(@RequestParam String username,
                                @RequestParam String yhzid,
                                HttpServletRequest httpServletRequest) {
        if (usersService.getusersByusername(username) != null)
            return false;
        Users users = new Users();
        users.setUsername(username);
        users.setGroupid(Integer.parseInt(yhzid));
        users.setRegtime(new Date(new Date().getTime()));
        users.setUpassword(UtilPacket.Md5MD5String("123456"));
        if (usersService.insertSelective(users) != 0) {
            setLog.setlod(httpServletRequest, "成功创建账户为" + username + "的账号", logService);
            return true;
        } else
            return false;
    }

    /*添加活动记录*/
    @RequestMapping("Inserthdjlgl")
    public boolean Inserthdjlgl(@RequestParam String aname,
                                @RequestParam String adate,
                                @RequestParam String axx,
                                @RequestParam String add,
                                @RequestParam String alx,
                                @RequestParam int ssuid,
                                HttpServletRequest httpServletRequest) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = sdf.parse(adate);
        Activity activity = new Activity();
        activity.setAname(aname);
        activity.setAdate(date);
        activity.setAxx(axx);
        activity.setAdd(add);
        activity.setAlx(alx);
        activity.setSsuid(ssuid);
        if (activityService.insertActivity(activity) != 0) {
            setLog.setlod(httpServletRequest, "成功创建活动名为" + aname + "的活动", logService);
            return true;
        } else
            return false;
    }

    /*教师添加学生成绩*/
    @RequestMapping("Insertxscjgl")
    public boolean Insertxscjgl(@RequestParam String grade,
                                @RequestParam int sskid,
                                @RequestParam int sssid,
                                @RequestParam int st,
                                @RequestParam String khfs,
                                @RequestParam String ksxz,
                                HttpServletRequest httpServletRequest) throws ParseException {
        Map paramMap = new HashMap();
        paramMap.put("sskid",sskid);
        paramMap.put("sssid",sssid);
        paramMap.put("st",st);
        if (gradeService.getGradeByKandS(paramMap)!=null)
            return false;
        List<Kcbl> kcbls = kcblService.getkcbllist();
        double dybl = 0.3;
        double tybl = 0.1;
        double stbl = 1;
        double zhbl = 0.6;
        if (kcbls!=null){
            for(Kcbl kcbl:kcbls){
                String bl = kcbl.getBl();
                String name = kcbl.getKname();
                if (name.equals("德育")){
                    dybl = Double.parseDouble(bl);
                }
                if (name.equals("体育")){
                    tybl = Double.parseDouble(bl);
                }
                if (name.equals("素拓")){
                    stbl = Double.parseDouble(bl);
                }
                if (name.equals("综合成绩学分绩点")){
                    zhbl = Double.parseDouble(bl);
                }
            }
        }
        Grade grade1 = new Grade();
        int xf = kechengService.getKechengById(sskid).getXf();
        String kname = kechengService.getKechengById(sskid).getKname();
        //获取用户输入成绩信息
        grade1.setGrade(grade);
        grade1.setSskid(sskid);
        grade1.setSssid(sssid);
        grade1.setSt(st);
        grade1.setKhfs(khfs);
        grade1.setKsxz(ksxz);
        DecimalFormat df = new DecimalFormat("#.00");
        if (!kname.equals("素拓")||!kname.equals("德育")||!kname.equals("体育")) {
            double jtxfjd=0;
            if (Double.parseDouble(grade)>=60)
                jtxfjd = (Double.parseDouble(grade)-50)/10;
            else jtxfjd=0;
            grade1.setXfjd(df.format(jtxfjd));
        }
        if (gradeService.insertGrade(grade1) != 0) {
            setLog.setlod(httpServletRequest, "成功创建课程id为" + sskid + "的成绩", logService);
            //开始判断是否有该学生本学期综合成绩记录
            Map sinfomap = new HashMap();
            sinfomap.put("ssuid",st);
            sinfomap.put("st",st);
            sinfomap.put("sssid",sssid);
            List<Grade> grades = gradeService.getGradeList(sinfomap);
            Zhcj zhcj = zhcjService.getZhcjByuands(sinfomap);
            //以下为存在此学生成绩的情况进行更新
            if (zhcj!=null) {
                if (kname.equals("素拓"))
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(grade) * stbl));
                else if (kname.equals("德育")) {
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(grade) * dybl));
                    zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) + Double.parseDouble(grade) * dybl));
                } else if (kname.equals("体育")) {
                    zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(grade) * tybl));
                    zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) + Double.parseDouble(grade) * tybl));
                } else {
                    double stcj=0;
                    double dycj=0;
                    double tycj=0;
                    double zcj=0;
                    double zxf=0;
                    int dkxf=0;
                    if (grades!=null){
                        for (Grade grade2:grades){
                            int kid = grade2.getSskid();
                            Kecheng kecheng = kechengService.getKechengById(kid);
                            String knamestr = "已删除该课程";
                            if (kecheng!=null) {
                                knamestr = kecheng.getKname();
                                dkxf = kecheng.getXf();
                            if (knamestr.equals("素拓")){
                                stcj = Double.parseDouble(grade2.getGrade());
                            }
                           else if (knamestr.equals("德育")){
                                dycj = Double.parseDouble(grade2.getGrade());
                            }
                           else if (knamestr.equals("体育")){
                                tycj = Double.parseDouble(grade2.getGrade());
                            }
                           else if (knamestr.equals("已删除该课程")){
                                continue;
                            }
                            else {
                                zcj=zcj+Double.parseDouble(grade2.getGrade())*dkxf;//总成绩
                                zxf=zxf+dkxf;//总学分
                            }
                            }
                        }
                    }
                    double zxfjd=0.0;
                    if (zxf!=0)
                        zxfjd=zcj/zxf;//总学分绩点
                    zhcj.setZhcj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl+stcj*stbl));
                    zhcj.setYscj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl));
                    zhcj.setZxfjd(df.format(zxfjd));
                }
                if (zhcjService.upZhcj(zhcj) != 0)
                    setLog.setlod(httpServletRequest, "学生" + st + "的综合成绩已更新", logService);
            }
            //如果没有此学生则创建综合成绩
            else {
                Zhcj newzhcj = new Zhcj();
                newzhcj.setSssid(sssid);
                newzhcj.setSsuid(st);
                newzhcj.setSszid(1);
                if (kname.equals("素拓"))
                    newzhcj.setZhcj(String.valueOf(Double.parseDouble(grade) * stbl));
                else if (kname.equals("德育")) {
                    newzhcj.setZhcj(String.valueOf(Double.parseDouble(grade) * dybl));
                    newzhcj.setYscj(String.valueOf(Double.parseDouble(grade) * dybl));
                } else if (kname.equals("体育")) {
                    newzhcj.setZhcj(String.valueOf(Double.parseDouble(grade) * tybl));
                    newzhcj.setYscj(String.valueOf(Double.parseDouble(grade) * tybl));
                } else {//计算其它课程总学分，总成绩
                    double stcj=0;
                    double dycj=0;
                    double tycj=0;
                    double zcj=0;
                    double zxf=0;
                    int dkxf=0;
                    if (grades!=null){
                        for (Grade grade2:grades){
                            int kid = grade2.getSskid();
                            Kecheng kecheng = kechengService.getKechengById(kid);
                            String knamestr = "已删除该课程";
                            if (kecheng!=null) {
                                knamestr = kecheng.getKname();
                                dkxf = kecheng.getXf();
                            if (knamestr.equals("素拓")){
                                stcj = Double.parseDouble(grade2.getGrade());
                            }
                            else if (knamestr.equals("德育")){
                                dycj = Double.parseDouble(grade2.getGrade());
                            }
                            else if (knamestr.equals("体育")){
                                tycj = Double.parseDouble(grade2.getGrade());
                            }
                            else if (knamestr.equals("已删除该课程")){
                                continue;
                            }
                            else {
                                zcj=zcj+Double.parseDouble(grade2.getGrade())*dkxf;
                                zxf=zxf+dkxf;
                            }
                            }
                        }
                    }
                    double zxfjd=0;
                    if (zxf!=0)
                        zxfjd=zcj/zxf;
                    newzhcj.setZhcj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl+stcj*stbl));
                    newzhcj.setYscj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl));
                    newzhcj.setZxfjd(df.format(zxfjd));
                }
                if (zhcjService.insertZhcj(newzhcj)!=0)
                    setLog.setlod(httpServletRequest, "学生" + st + "的综合成绩已创建", logService);
            }
            return true;
        } else
            return false;
    }

    /*添加菜单*/
    @RequestMapping("Insertcdgl")
    public boolean Insertcdgl(@RequestParam(value = "menuname") String menuname,
                              @RequestParam(value = "menulink") String menulink,
                              @RequestParam(value = "fdm") String fdm,
                              HttpServletRequest httpServletRequest) {
        Menu menu = new Menu();
        if (menuname != "")
            menu.setMenulink(menulink);
        menu.setMenuname(menuname);
        if (fdm != "")
            menu.setFdm(fdm);
        if (menuService.insertSelective(menu) != 0) {
            setLog.setlod(httpServletRequest, "添加名字为" + menuname + "的菜单", logService);
            return true;
        } else
            return false;
    }

}
